//
//  ViewController.swift
//  TestSocket
//
//  Created by yxl on 2022/8/10.
//

import UIKit
import SocketIO


class ViewController: UIViewController {
    
    var textView : UITextView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        /*
         let SSLSecu = SSLSecurity(usePublicKeys: true)
         
         config = [
             .log(false),//打印日志
             .compress,
             .connectParams(["source":3, "Authorization":"AUTHORIZATION_TOKEN_11002552_932771a41f344a3fa0bec1b6924a81a8", "uid":"11002552"]),
             .extraHeaders(socketHeaders),
             .reconnects(false), //是否重连机制
             .reconnectWaitMax(7), //重新连接等待最大秒数
             .forceWebsockets(true),
             .forcePolling(true),
             .forceNew(true),
             .reconnectAttempts(5),
             .selfSigned(true),
             .security(SSLSecu),
         ]
         */
        
        // 注册
        SocketUtil.share.connect(authorization: "sasasa", uid: "001")
        //创建视图
        let btn = UIButton.init(type: .custom);
        btn.frame = CGRect(x: 0, y: 0, width: 100, height: 50)
        btn.center = view.center;
        btn.setTitle("发送", for: .normal)
        btn.backgroundColor = UIColor.red;
        view.addSubview(btn);
        btn.addTarget(self, action: #selector(btnClicked), for: .touchUpInside)
        
        textView = UITextView.init(frame: CGRect(x: 10, y: 200, width: self.view.frame.width - 20, height: 100))
        textView?.textColor = .black
        textView?.backgroundColor = .orange
        textView?.font = .systemFont(ofSize: 15)
        view.addSubview(textView!)
    }
    
    @objc func btnClicked() {
        var dic : [String: Any] = [:]
        dic["category"] = 2
        dic["content"] = self.textView?.text ?? "temp"
        dic["roomId"] = "room_001"
        dic["toUid"] = "002"
        dic["type"] = 1
        dic["uid"] = "001"
        SocketUtil.share.chatMessageSend(.message, params: dic)
    }
    
}

